/**
 * 
 */
var banks = [
	'工商银行',
	'建设银行',
	'中国银行',
	'农业银行',
	'交通银行',
	'邮政银行',
	'招商银行',
	'兴业银行',
	'中信银行',
	'光大银行',
	'民生银行',
	'广发银行',
	'华夏银行',
	'浦发银行',
	'平安银行'
	];

var v_agent_add = new Vue({
	el: '#agentAddPage',
	data: {
		title: '添加代理',
		needBack: false,
		needRefresh: false,
		show: false,
		recommendAgentId: '',
		recommend: true,
		form: {
			refereeId: '',
			refereeName: '',
			name: '',
			mobileNo: '',
			captchaCode: '',
			password: '',
			password2: '',
			email: '',
			signature: '',
			bank: '',
			bankCard: '',
			zhifubao: '',
			wxPay: ''
		}
	},
	created: function() {
		this.$nextTick(function() {
			if ($('#agentAddPage').length > 0) {
				this.init();
			} else {
				this.needBack = true;
			}
		});
	},
	updated: function() {
		// 初始化银行的选择，虽然updated就要执行一次，不过放在其他地方明显的不会执行啊
		$(v_agent_add.$el).find("#bankPicker").picker({
			toolbarTemplate: $('#bankPickerDiv').html(),
			cols: [{
				textAlign: 'center',
				values: banks
			    }],
			onClose: function() {
				v_agent_add.form.bank = $(this.input).val();
			}
		});
	},
	methods: {
		init: function() {
			// 初始化推荐人
			var url = self.location.pathname;
			var requestUrl = '/agent/current';
			if (url.indexOf('/center') == 0) {
				this.recommend = false;
			} else if (url.indexOf('/recommend') == 0) {
				this.recommendAgentId = url.substring(url.lastIndexOf('/') + 1, url.length);
				requestUrl = '/agent/recommend/' + this.recommendAgentId;
			}
			axios.post(requestUrl)
			.then(function(resp) {
				if (resp.data.success) {
					v_agent_add.form.refereeId = resp.data.message.id;
					v_agent_add.form.refereeName = resp.data.message.name;
				}
			})
			.catch(function (error) {
				console.log(error);
			});
		},
		sendCode: function() {
			var mobile = this.form.mobileNo;
			if (mobile) {
				var $modal = $.modal({
					title:  '发送短信验证码',
					text:$('#sendCodeDiv').html(),
					buttons: [{
				    	text: '取消',
				    	close: true
					}, {
				    	text: '发送',
				    	bold: true,
				    	close: false,
				    	onClick: function() {
				    		var code = $('div.modal.modal-in').find('input').val();
				    		axios.post('/agent/send/sms', {mobileNo: mobile, code: code, captchaName: 'agent_regist_code'})
							.then(function(resp) {
								$.hidePreloader();
								if (resp.data.success) {
									$.toast("发送成功。");
									$.closeModal($modal);
								} else {
									$.toast(resp.data.message);
									v_agent_add.changeImageCaptcha();
								}
							})
							.catch(function (error) {
								console.log(error);
							});
				    	}
				    }]
				});
				$('div.modal.modal-in').find('img').off('click').on('click', function() {
					v_agent_add.changeImageCaptcha();
				});
				v_agent_add.changeImageCaptcha();
			} else {
				 $.alert('请输入手机号码');
			}
		},
		goback: function() {
			// 返回
		},
		changeImageCaptcha: function() {
			$('div.modal.modal-in').find('img').attr('src', '/captcha/get/agent_regist_code?' + new Date().getTime());
		},
		postInfo: function() {
			// 初始化推荐人
			var param = $.extend({
				aesId: v_agent_add.recommendAgentId
			}, this.form);
			var requestUrl = v_agent_add.recommend ? '/agent/recommend/sub/add' : '/agent/sub/add';
			axios.post(requestUrl, param)
			.then(function(resp) {
				if (resp.data.success) {
					$.alert('已成功添加代理，管理员将尽快完成审核。', '操作成功', function () {
						if (v_agent_add.recommend) {
							self.location.href = '/';
						} else {
							$.each(v_agent_add.form, function(key) {
								v_agent_add.form[key] = '';
							});
							$.router.load('#centerPage');
						}
			        });
				} else {
					if ($.isArray(resp.data.message) && resp.data.message.length > 1) {
						$.alert(resp.data.message.join('<br>'));
					} else {
						$.toast(resp.data.message);
					}
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		}
	}
});
